package org.iweb.init.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iweb.init.AbstractRegister;
import org.iweb.init.Register;
import org.iweb.init.scanner.AnnotationScanner;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Register(name="IWebRegister")
public class IWebAppRegister extends AbstractRegister{

	private Map<String,IWebApplication> instances;
	
	
	@Override
	public void populate(AnnotationScanner scanner) {
		List<Class> annotatedClasses= scanner.getListByAnnotation(IWebApp.class);		
		for(Class c: annotatedClasses){				
			IWebApp ann=(IWebApp) c.getAnnotation(IWebApp.class);			
			try {
				getInstances().put(ann.value(),(IWebApplication) c.newInstance());
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}								
		}
		
//		InjectObj bobj=null;
//		
//		for(String id: getInstances().keySet()){			
//			bobj=getInstances().get(id);			
//			if(!bobj.isLazy()){				
//				ServiceLookup.getInstance().get(id);				
//			}			
//		}		
		
	}
	
	public Map<String,IWebApplication> getInstances() {
		if(instances==null)
			instances=new HashMap<String,IWebApplication>();
		return instances;
	}

	@Override
	public void clear() {
		getInstances().clear();		
	}

	@Override
	public Long getNumReg() {
		return new Long(getInstances().size());
	}

}
